import FYLog from "./FYLog";

/** 资源管理器 */
export default class FYResMgr {
    /** 单例 */
    public static readonly Instance: FYResMgr = new FYResMgr();
    private constructor() { }

    //#region ------------------- 同步加载 -----------------------

    /**
     * 同步加载资源
     * @param resPath 资源地址
     */
    public loadResSync(resPath: string, type: typeof cc.Asset) {
        return new Promise<any>((resolve, reject) => {
            cc.loader.loadRes(resPath, type, (err, resObj) => {
                if (err != null) {
                    FYLog.error(err + " " + resPath);
                    return;
                }
                resolve(resObj);
            });
        });
    }

    /**
     * 同步加载预制
     * @param prefabName 预制名 
     */
    public loadPrefabResSync(prefabName: string) {
        let resPath = "Prefab/UI/" + prefabName;
        return this.loadResSync(resPath, cc.Prefab);
    }

    /**
     * 同步加载图片
     * @param spriteFrameName 图片名
     */
    public loadSpriteFrameSync(spriteFrameName: string) {
        let resPath = "AutoAtlas/" + spriteFrameName;
        return this.loadResSync(resPath, cc.SpriteFrame);
    }

    /**
     * 同步加载特效
     * @param effectName 特效名字
     */
    public loadEffectSync(effectName: string) {
        let resPath = "Prefab/Effect/" + effectName;
        return this.loadResSync(resPath, cc.Prefab);
    }

    /**
     * 同步加载序列帧
     * @param sequenceFrameName 序列帧名字
     */
    public loadSequenceFrameSync(sequenceFrameName: string) {
        let resPath = "Prefab/SequenceFrame/" + sequenceFrameName;
        return this.loadResSync(resPath, cc.Prefab);
    }

    /**
     * 同步加载其他
     * @param otherName 其他名字
     */
    public loadOtherSync(otherName: string) {
        let resPath = "Prefab/Other/" + otherName;
        return this.loadResSync(resPath, cc.Prefab);
    }

    /**
     * 同步加载音乐
     * @param musicName 音乐名字
     */
    public loadMusicSync(musicName: string) {
        let resPath = "Audio/Music/" + musicName;
        return this.loadResSync(resPath, cc.AudioClip);
    }

    /**
     * 同步加载声音
     * @param soundName 声音名字
     */
    public loadSoundSync(soundName: string) {
        let resPath = "Audio/Sound/" + soundName;
        return this.loadResSync(resPath, cc.AudioClip);
    }

    //#endregion
}
